<?php
/*
@author: wjf
@contact: Hurrican
@software: PhpStorm
@create: 2022/8/9 9:12
*/
declare(strict_types=1);
namespace app\store\controller;

use think\App;
use think\response\Json;
use app\store\service\CompanyUser as CompanyUserService;



class CompanyUser extends Controller{

    private $companyUserService;
    public function __construct(App $app,CompanyUserService $companyUserService)
    {
        parent::__construct($app);
        $this->companyUserService = $companyUserService;
    }

    //列表
    public function index():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'page' => 'integer',
                'limit' => 'integer',
            ]
        );
        $list = $this->companyUserService->getIndex($params);
        return $this->renderSuccess(compact('list'));
    }

    //新增
    public function add():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'name' => 'require',
                'contract' => 'require',
                'balance' => 'require',
                'ratio|换算比例' => 'integer',
               // 'address|地址' => ''
            ]
        );
        $res = $this->companyUserService->add($params);
        if(!$res){
            return $this->renderError('系统异常');
        }
        return $this->renderSuccess([],'操作成功');

    }

    public function edit():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'id' => 'require',
                'name' => 'require',
                'contract' => 'require',
                'balance' => 'require',
                'ratio|换算比例' => 'integer',
                // 'address|地址' => ''
            ]
        );
        $res = $this->companyUserService->edit($params);
        if(!$res){
            return $this->renderError('系统异常');
        }
        return $this->renderSuccess();

    }


    //删除
    public function delete():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'id' => 'require',
            ]
        );
        $id = $params['id'];
        $res = $this->companyUserService->delete($id);
        if(!$res){
            return $this->renderError('系统异常');
        }
        return $this->renderSuccess([],'删除成功');

    }


}